package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.ProductCon;
import cn.wolfcode.trip.base.query.ProductQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IProductDirService;
import cn.wolfcode.trip.base.service.IProductService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述：商品控制器
 *
 * @package: cn.wolfcode.trip.admin.controller
 * @author: Patch
 * @date: 2019/5/31 17:22
 * 总结:
 */
@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IProductDirService dirService;

    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") ProductQueryObject qo) {
        // 设置排序
        qo.setOrderBy("p.sequence asc");
        model.addAttribute("pageInfo", productService.query(qo));
        // 查询所有的商品父类型
        model.addAttribute("parents", dirService.listParentAll());
        return "shop/product/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Product product) {
        JsonResult result = new JsonResult();
        try {
           productService.saveOrUpdate(product);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("亲，操作失败，请重试");
        }
        return result;
    }

    /**
     * 查询商品详情
     */
    @RequestMapping("getContentById")
    @ResponseBody
    public ProductCon getContentById(Long id) {
        return productService.getContentById(id);
    }

    /**
     * 删除指定商品
     */
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            productService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
